package com.android.uit;

import java.util.ArrayList;
import java.util.List;


import com.android.uit.QuickAction.OnActionItemClickListener;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.PopupWindow;

public class ListMusicActivity extends ListActivity {
    /** Called when the activity is first created. */
        private Cursor myCursor;
        ListView listview;
        private int mSelectedRow = 0;
    private final int menu_Logout=0;
    private final int menu_Backup=1;
    private final int menu_MarkAll=2;
	private ImageView mMoreIv = null;
    private listmusicAdapter mAdapter;
    private Context context;
    private AlertDialog.Builder dialog;
    private EditText edt_Name;
    private final List<Music> listMusic = new ArrayList<Music>();
    private ArrayList<Boolean> itemCheck=new  ArrayList<Boolean>(); 
    private ArrayList<String> itemString= new ArrayList<String>();
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listview =getListView();
        context=getBaseContext();
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
        dialog = new AlertDialog.Builder(this);
        edt_Name= new EditText(this);
        dialog.setView(edt_Name);
        myCursor=this.managedQuery(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
     
        startManagingCursor(myCursor);
        while (myCursor.moveToNext())
        {
        	String id= myCursor.getString(myCursor.getColumnIndexOrThrow(Audio.Media._ID));
            String musicName = myCursor.getString(myCursor.getColumnIndexOrThrow(Audio.Media.DISPLAY_NAME));
            String musicArtist= myCursor.getString(myCursor.getColumnIndexOrThrow(Audio.Media.ARTIST));
            listMusic.add(new Music(id,musicName, musicArtist));
        }
        mAdapter= new listmusicAdapter(this,listMusic);
        this.setListAdapter(mAdapter);
        // lay trang thai cac checkbox
        itemCheck=mAdapter.getCheckList();
     //-----------------xu ly quick action item----------------
        ActionItem openItem = new ActionItem(1, "Open", getResources().getDrawable(R.drawable.ic_add));

        ActionItem deleteItem = new ActionItem(2, "Delete", getResources().getDrawable(R.drawable.ic_accept));
        final QuickAction mQuickAction= new QuickAction(context);
        mQuickAction.addActionItem(openItem);
        mQuickAction.addActionItem(deleteItem);
        mQuickAction.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			public void onItemClick(QuickAction quickAction, int pos, int actionId) {
				// TODO Auto-generated method stub
				ActionItem actionitem= quickAction.getActionItem(pos);
				if(actionId==1)
				{
					Toast.makeText(context, "open item"+mSelectedRow, Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(context, "delete item"+mSelectedRow, Toast.LENGTH_SHORT).show();
			}
		});
       listview.setOnItemClickListener(new OnItemClickListener() 
       {
           @Override
           public void onItemClick(AdapterView<?> arg0, View view, int position,
                        long arg3) {
                   // TODO Auto-generated method stub
        	   mSelectedRow=position;
        	   mQuickAction.show(view);
        	//	mMoreIv = (ImageView) view.findViewById(R.id.i_more);
			//	mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
           
           }
       });
       
//       mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() {			
//			@Override
//			public void onDismiss() {
//				mMoreIv.setImageResource(R.drawable.ic_list_more);
//			}
//		});
    }
           public void showToast(String message)
       {
                Toast.makeText(this,message , Toast.LENGTH_SHORT).show();
       }
           public boolean onCreateOptionsMenu(Menu menu) {
                // TODO Auto-generated method stub
                //return super.onCreateOptionsMenu(menu);
                 menu.add(0, menu_Logout, 3,"Đăng Xuất").setIcon(R.drawable.ic_launcher);
                 menu.add(0, menu_Backup, 2,"Sao Lưu").setIcon(R.drawable.ic_launcher);
                 menu.add(0, menu_MarkAll,1, "Chon Hết").setIcon(R.drawable.ic_launcher);
                        return true;
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // TODO Auto-generated method stub
                switch(item.getItemId())
                {
                        case menu_Backup:
                                //xu ly backup
//                                itemString.clear();
//                                String path= null;
//                                for(int i=0;i<itemCheck.size();i++)
//                                {
//                                        // kiem tra mang item check , neu la
//                                        //true thi lay vi tri va truyen vao 
//                                        //cursor
//                                        if(itemCheck.get(i)==true)
//                                        {
//                                int index = myCursor.getColumnIndexOrThrow(Audio.Media.DATA);                   
//                                myCursor.moveToPosition(i);
//                                path=myCursor.getString(index);
//                                //add vao mang itemString
//                                                itemString.add(path);
//                                                Log.d("Chuoi item", path); 
//                                                showToast(path);
//                                        }
//                                }
                        	
                        	
                        	// xu ly dat ten file
                        /*	dialog.setMessage("Nhap ten file").setTitle("Thong bao").setNegativeButton("OK", new OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									String temp= edt_Name.getText().toString().trim();
									Toast.makeText(context, temp,Toast.LENGTH_SHORT).show();
								}

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									
								}
							}).setNegativeButton("No", new OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									dialog.cancel();
								}

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									
								}
							});*/
                        	dialog.show();
						
                                return true;
                        case menu_Logout:
                                //xu ly logout
                                finish();
                                return true;
                        case menu_MarkAll:
                                //xu ly check all
                                for(int i=0;i<itemCheck.size();i++)
                               {
                                          itemCheck.set(i, true);
                                  mAdapter.setCheckList(itemCheck);
                                  
                               }
                                this.setListAdapter(mAdapter);
                                return true;
                }
                return false;
            }
}
